var RepairPartsChart = function () {
  var template = /* html */ `
    <div class="repair-parts-chart">
      <div ref="chart" class="repair-parts-chart-div"></div>
      <div class="repair-parts-chart-table">
        <table cellspacing="0" cellpadding="0">
          <tr>
            <th>部件</th>
            <td v-for="parts in partsRepairData" :key="'name_'+parts.id">
              {{parts.name}}
            </td>
          </tr>
          <tr>
            <th>维修次数</th>
            <td v-for="parts in partsRepairData" :key="'count_'+parts.id">
              {{parts.count}}
            </td>
          </tr>
          <tr>
            <th>占比</th>
            <td v-for="parts in partsRepairData" :key="'rate_'+parts.id">
              {{parts.rate}}%
            </td>
          </tr>
        </table>
      </div>
    </div>
  `;

  return {
    props: {
      repairData: {
        type: Object,
        required: true
      }
    },

    watch: {
      partsRepairData: function () {
        this.clearTimeout();
        this.resetChart();
        this.chartRunning();
      }
    },

    computed: {
      partsRepairData: function () {
        return this.repairData.partsRepairData;
      }
    },

    data: function () {
      return {
        timeout: null
      };
    },

    mounted: function () {
      this.showChart();

      var self = this;
      $(this.$el).resize(function () {
        var chartDom = self.$refs['chart'];
        if (chartDom === undefined) {
          return;
        }
        var chart = echarts.getInstanceByDom(chartDom);
        if (chart !== undefined) {
          chart.resize();
        }
      });
      this.chartRunning();
    },

    beforeDestroy: function () {
      this.clearTimeout();
    },

    methods: {
      getChart: function () {
        var chartDom = this.$refs['chart'];
        var chart = echarts.getInstanceByDom(chartDom);
        if (chart === undefined) {
          chart = echarts.init(chartDom);
        }
        return chart;
      },

      showChart: function () {
        var chart = this.getChart();
        var chartOption = this.getChartOption();
        this.resetChartOption(chartOption);
        chart.setOption(chartOption);
      },

      getChartOption: function () {
        var chartOption = {
          color: window.boardChartColor,
          grid: {
            right: 40,
            left: 40,
            bottom: 25,
            top: 40
          },
          legend: {
            type: 'scroll',
            top: 5,
            selectedMode: false,
            textStyle: {
              color: '#44D3E4',
              fontSize: 16
            },
            pageTextStyle: {
              color: '#44D3E4',
              fontSize: 16
            }
          },
          xAxis: [{
            type: 'category',
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            axisLabel: {
              color: '#fff',
              fontSize: 12,
              interval: 0,
              formatter: function (value) {
                if (typeof value !== 'string') {
                  return;
                }
                return value;
              }
            }
          }],
          yAxis: [{
            type: 'value',
            min: 0,
            max: 5,
            name: '次数',
            nameGap: 10,
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            axisLabel: {
              color: '#fff',
              fontSize: 12
            }
          }, {
            type: 'value',
            show: false,
            min: 0,
            max: 100
          }],
          tooltip: {
            trigger: 'axis',
            formatter: function (params) {
              var values = params[0].value;
              if (values.length !== 3) {
                return;
              }

              var name = params[0].name;
              var texts = [name];
              var unitObj = {
                "维修次数": "次",
                "占比": "%"
              };
              for (var i = 0; i < params.length; i++) {
                var param = params[i];
                var value = values[i + 1];
                var unit = unitObj[param.seriesName];
                texts.push(`${param.marker} ${param.seriesName}:${value}${unit}`);
              }
              return texts.join('<br>');
            }
          },
          dataset: {
            dimensions: this.repairData.partsDimensions,
            source: []
          },
          series: [{
            type: 'bar',
            name: '维修次数',
            encode: {
              x: 'name',
              y: 'count'
            }
          }, {
            type: 'bar',
            name: '占比',
            yAxisIndex: 1,
            encode: {
              x: 'name',
              y: 'rate'
            }
          }]
        };

        return chartOption;
      },

      resetChart: function () {
        var chart = this.getChart();
        var chartOption = {};
        this.resetChartOption(chartOption);
        chart.setOption(chartOption);
      },

      resetChartOption: function (chartOption) {
        var repairData = this.repairData;
        _.set(chartOption, 'dataset.source', repairData.getPartsRepairData());
        _.set(chartOption, 'yAxis[0].max', repairData.maxPartsCount);
      },

      chartRunning: function () {
        var self = this;
        this.clearTimeout();
        var timeout = setTimeout(function () {
          var repairData = self.repairData;
          var flag = repairData.incrementDayIndex();
          if (flag) {
            self.resetChart();
          }
          self.timeout = null;
          self.chartRunning();
        }, 10 * 1000);
        this.timeout = timeout;
      },

      clearTimeout: function () {
        var timeout = this.timeout;
        if (timeout !== null) {
          window.clearTimeout(timeout);
          this.timeout = null;
        }
      }
    },

    template: template
  };
}();
